<?php
class tahun_ajaran extends MY_Controller{
    function tahun_ajaran(){
        parent::MY_Controller();
        $this->data['title'] = "Administrasi Tahun Ajaran";
        $this->load->model("m_tahun_ajaran");
        $this->load->model("m_semester");
    }
    
    var $data;
    var $limit = 10;
    
    function index(){
        $this->get_tahun_ajaran();
    }
    function get_tahun_ajaran($offset = 0){
        $this->data['content'] = "tahun_ajaran/v_tahun_ajaran";
        
        $uri_segment = 3;
        if($offset != 0){
          $offset = $this->uri->segment($uri_segment);  
        }
        $num_rows = $this->m_tahun_ajaran->getCountRows();
        $this->data['listTahunAjaran'] = $this->m_tahun_ajaran->getListTahunAjaran($offset,$this->limit);
        $config['base_url'] = site_url('tahun_ajaran/get_tahun_ajaran');
        $config['total_rows'] = $num_rows;
        $config['per_page'] = $this->limit;
        $config['uri_segment'] = $uri_segment;
        $this->pagination->initialize($config);
        $this->data['pagination'] = $this->pagination->create_links();
        $this->data['offset'] = $offset;
        
        $this->render($this->data);
    }
    function add_tahun_ajaran_form($offset = 0){
        $this->data['content'] = "tahun_ajaran/v_tahun_ajaran";
        $this->data['subLink'] = "tahun_ajaran/add_tahun_ajaran_proses";
        $this->data['formTitle'] = "Tambah Data Tahun Ajaran";
        $this->data['formView'] = "tahun_ajaran/addTahunAjaranForm";
        
        $uri_segment = 3;
        if($offset != 0){
          $offset = $this->uri->segment($uri_segment);  
        }
        $num_rows = $this->m_tahun_ajaran->getCountRows();
        $this->data['listTahunAjaran'] = $this->m_tahun_ajaran->getListTahunAjaran($offset,$this->limit);
        $this->data['listSemester'] = $this->m_semester->getListSemester();
        $config['base_url'] = site_url('tahun_ajaran/add_tahun_ajaran_form');
        $config['total_rows'] = $num_rows;
        $config['per_page'] = $this->limit;
        $config['uri_segment'] = $uri_segment;
        $this->pagination->initialize($config);
        $this->data['pagination'] = $this->pagination->create_links();
        $this->data['offset'] = $offset;
        
        $this->render($this->data);
    }
    function edit_tahun_ajaran_form($id,$offset = 0){
        $this->data['content'] = "tahun_ajaran/v_tahun_ajaran";
        $this->data['subLink'] = "tahun_ajaran/edit_tahun_ajaran_proses";
        $this->data['formTitle'] = "Edit Data Tahun Ajaran";
        $this->data['formView'] = "tahun_ajaran/editTahunAjaranForm";
        
        $uri_segment = 4;
        if($offset != 0){
          $offset = $this->uri->segment($uri_segment);  
        }
        $num_rows = $this->m_tahun_ajaran->getCountRows();
        $this->data['dataInput'] = $this->m_tahun_ajaran->getTahunAjaranById($id);
        $this->data['listTahunAjaran'] = $this->m_tahun_ajaran->getListTahunAjaran($offset,$this->limit);
        $this->data['listSemester'] = $this->m_semester->getListSemester();
        $config['base_url'] = site_url('tahun_ajaran/edit_tahun_ajaran_form/'.$id);
        $config['total_rows'] = $num_rows;
        $config['per_page'] = $this->limit;
        $config['uri_segment'] = $uri_segment;
        $this->pagination->initialize($config);
        $this->data['pagination'] = $this->pagination->create_links();
        $this->data['offset'] = $offset;
        
        $this->render($this->data);
    }
    function add_tahun_ajaran_proses(){
        $data = array('nama' => $this->input->post('nama'),
            'id_semester' => $this->input->post('semester'),
            'tgl_mulai' => date2mysql($this->input->post('tglMulai')),
            'tgl_selesai' => date2mysql($this->input->post('tglSelesai')),
            'status' => '1');
        $insert = $this->m_tahun_ajaran->addTahunAjaran($data);
        if($insert == TRUE){
            $this->session->set_flashdata("message",1);
        }else{
            $this->session->set_flashdata("message",2);
        }
        
        redirect("tahun_ajaran");
    }
    function edit_tahun_ajaran_proses(){
        $data = array('id' => $this->input->post('id'),
            'nama' => $this->input->post('nama'),
            'id_semester' => $this->input->post('semester'),
            'tgl_mulai' => date2mysql($this->input->post('tglMulai')),
            'tgl_selesai' => date2mysql($this->input->post('tglSelesai')));
        $update = $this->m_tahun_ajaran->editTahunAjaran($data);
        if($update == TRUE){
            $this->session->set_flashdata("message",1);
        }else{
            $this->session->set_flashdata("message",2);
        }
        
        redirect("tahun_ajaran");
    }
    function delete_tahun_ajaran($id){
        $delete = $this->m_tahun_ajaran->deleteTahunAjaran($id);
        if($delete == TRUE){
            $this->session->set_flashdata("message",3);
        }else{
            $this->session->set_flashdata("message",4);
        }
        
        redirect("tahun_ajaran");
    }
    function aktivasi($id,$val){
        $data = array('id' => $id,'status' => "$val");
        $aktivasi = $this->m_tahun_ajaran->aktivasiTahunAjaran($data);
        if($update == TRUE){
            $this->session->set_flashdata("message",1);
        }else{
            $this->session->set_flashdata("message",2);
        }
        
        redirect('tahun_ajaran');
    }
}
?>
